package com.atguigu.gulimall.order.controller;


import com.atguigu.gulimall.order.entity.OrderEntity;
import com.atguigu.gulimall.order.entity.OrderReturnApplyEntity;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.UUID;

@Slf4j
@RestController
public class DealQueueController {

    @Autowired
    RabbitTemplate rabbitTemplate;


    @GetMapping("/sendMsg")
    public String result(@RequestParam(value = "number", defaultValue = "10") Integer number) {
        for (int i = 0; i < number; i++) {
            if (i % 2 == 0) {
                OrderReturnApplyEntity orderReturnApplyEntity = new OrderReturnApplyEntity();
                orderReturnApplyEntity.setId(1L);
                orderReturnApplyEntity.setCompanyAddress("常州无锡金坛" + i);
                orderReturnApplyEntity.setCreateTime(new Date());
                String msg = "用代码发送消息";
                //使发送对象的消息是一个json格式的数据
//                rabbitTemplate.convertAndSend("hello-java-exchange", "hello.java", orderReturnApplyEntity);
                rabbitTemplate.convertAndSend("hello-java-exchange",
                        "hello.java",
                        orderReturnApplyEntity,
                        new CorrelationData(UUID.randomUUID().toString()));
                log.info("消息发送成功  OrderReturnApplyEntity{}  " + i);
            } else {
                OrderEntity orderEntity = new OrderEntity();
                orderEntity.setId(1L);
                orderEntity.setBillContent("常州无锡金坛" + i);
                orderEntity.setCreateTime(new Date());
                String msg = "用代码发送消息";
                //使发送对象的消息是一个json格式的数据
                rabbitTemplate.convertAndSend("hello-java-exchange", "hello.java", orderEntity);
                log.info("消息发送成功  OrderEntity {} " + i);
            }
        }
        return "ok";

    }


}
